/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Giveaway;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.service.GiveawayService;
import com.hboxs.asl.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Controller - 企业服务
 */
@Controller("adminGiveawayController")
@RequestMapping("/asl-admin/giveaway")
public class GiveawayController extends BaseController {

    @Resource(name = "giveawayServiceImpl")
    private GiveawayService giveawayService;
    @Resource(name = "productServiceImpl")
    private ProductService productService;

    /**
     * 获取商品
     *
     * @param sn
     * @return
     */
    @RequestMapping(value = "/product", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage product(String sn) {
        try {
            Product product = productService.findBySn(sn.trim());

            return JsonMessage.success(product.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/admin/giveaway/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Giveaway giveaway, String sn, RedirectAttributes redirectAttributes) {
        if (!isValid(giveaway)) {
            return ERROR_VIEW;
        }
        // 活动开始日期为空时，表示当前日期
        Date startDate = giveaway.getStartDate();
        giveaway.setStartDate(startDate != null ? startDate : new Date());
        giveaway.setProduct(productService.findBySn(sn));
        String title = giveaway.getTitle();
        giveaway.setTitle(title != null ? title : "充值满 " + giveaway.getQuota() + " 元赠品");
        giveawayService.save(giveaway);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("giveaway", giveawayService.find(id));
        return "/admin/giveaway/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Giveaway giveaway, String sn, RedirectAttributes redirectAttributes) {
        if (!isValid(giveaway)) {
            return ERROR_VIEW;
        }
        // 活动开始日期为空时，表示当前日期
        Date startDate = giveaway.getStartDate();
        giveaway.setStartDate(startDate != null ? startDate : new Date());
        giveaway.setProduct(productService.findBySn(sn));
        String title = giveaway.getTitle();
        giveaway.setTitle(title != null ? title : "充值满 " + giveaway.getQuota() + " 元赠品");
        giveawayService.update(giveaway, "");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", giveawayService.findPage(pageable));
        return "/admin/giveaway/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        giveawayService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
